<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul {
            list-style: none;
        }

        a {
            text-decoration: none;
            color: #666;
        }

        body {
            background: #fff;
            color: #666;
            font-size: 14px;
        }

        input {
            outline: none;
        }

        .clearfix::before,
        .clearfix::after {
            content: '';
            display: block;
            clear: both;
        }

        .clearfix {
            *zoom: 1
        }

        /* 引入购物车样式 */
        table {
            width: 800px;
            margin: 0 auto;
            border-collapse: collapse;
        }

        th {
            font: normal 14px/50px;
            color: #666;
        }

        th,
        td {
            border: none;
            text-align: center;
            border-bottom: 1px dashed #ccc;
        }

        input[type='checkbox'] {
            width: 13px;
            height: 13px;
        }

        tbody p {
            position: relative;
            bottom: 10px;
        }

        tbody .add,
        tbody .reduce {
            float: left;
            width: 22px;
            height: 22px;
            border: 1px solid #ccc;
            text-align: center;
            background: none;
            outline: none;
            cursor: pointer;
        }

        tbody input[type='text'] {
            width: 50px;
            float: left;
            height: 18px;
            text-align: center;
        }

        tbody .count-c {
            width: 98px;
            margin: 0 auto;
        }

        button[disabled] {
            color: #ddd;
            cursor: not-allowed;
        }

        tbody tr:hover {
            background: #eee;
        }

        tbody tr.active {
            background: rgba(241, 209, 149, 0.945);
        }

        .controls {
            width: 790px;
            margin: 10px auto;
            border: 1px solid #ccc;
            line-height: 50px;
            padding-left: 10px;
            position: relative;
        }

        .controls .del-all,
        .controls .clear {
            float: left;
            margin-right: 100px;
        }

        .controls span {
            color: red;
        }

        .controls .pay {
            position: absolute;
            right: 0;
            width: 80px;
            height: 54px;
            background: red;
            font: bold 20px/54px '宋体';
            color: #fff;
            text-align: center;
            bottom: -1px;
        }

        .controls .total-price {
            font-weight: bold;
        }

        img {
            width: 100px;
            height: 100px;
        }
    </style>
</head>

<body>
    <div class="car">
        <table>
            <thead>
                <tr>
                    <th><input type="checkbox" id="all">全选</th>
                    <th>商品</th>
                    <th>单价</th>
                    <th>商品数量</th>
                    <th>小计</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody id="carBody">
                <tr>
                    <td><input type="checkbox" class="s_ck" readonly></td>
                    <td>
                        <img src="/imgs/timthumb(1).jpg" alt="">
                        <p>牛奶</p>
                    </td>
                    <td class="price">5¥</td>
                    <td>
                        <div class="count-c clearfix">
                            <button class="reduce" disabled>-</button>
                            <input type="text" value="1">
                            <button class="add">+</button>
                        </div>
                    </td>
                    <td class="total">25¥</td>
                    <td><a href="javascript:;" class="del">删除</a></td>
                </tr>
                <tr>
                    <td><input type="checkbox" class="s_ck" readonly></td>
                    <td>
                        <img src="/imgs/timthumb(2).jpg" alt="">
                        <p>牛奶</p>
                    </td>
                    <td class="price">5¥</td>
                    <td>
                        <div class="count-c clearfix">
                            <button class="reduce" disabled>-</button>
                            <input type="text" value="1">
                            <button class="add">+</button>
                        </div>
                    </td>
                    <td class="total">5¥</td>
                    <td><a href="javascript:;" class="del">删除</a></td>
                </tr>
                <tr>
                    <td><input type="checkbox" class="s_ck" readonly></td>
                    <td>
                        <img src="/imgs/timthumb(3).jpg" alt="">
                        <p>牛奶</p>
                    </td>
                    <td class="price">10¥</td>
                    <td>
                        <div class="count-c clearfix">
                            <button class="reduce" disabled>-</button>
                            <input type="text" value="1">
                            <button class="add">+</button>
                        </div>
                    </td>
                    <td class="total">20¥</td>
                    <td><a href="javascript:;" class="del">删除</a></td>
                </tr>
                <tr>
                    <td><input type="checkbox" class="s_ck" readonly></td>
                    <td>
                        <img src="/imgs/timthumb(4).jpg" alt="">
                        <p>牛奶</p>
                    </td>
                    <td class="price">5¥</td>
                    <td>
                        <div class="count-c clearfix">
                            <button class="reduce" disabled>-</button>
                            <input type="text" value="1">
                            <button class="add">+</button>
                        </div>
                    </td>
                    <td class="total">30¥</td>
                    <td><a href="javascript:;" class="del">删除</a></td>
                </tr>
            </tbody>
        </table>
        <div class="controls clearfix">
            <a href="javascript:;" class="del-all">删除所有商品</a>
            <a href="javascript:;" class="clear">清理购物车</a>
            <a href="javascript:;" class="pay">去结算</a>
            <p>
                已经选中<span id="totalCount">0</span>件商品;总价<span id="totalPrice" class="total-price">0¥</span>
            </p>
        </div>
    </div>
    <script>
        let adds = document.querySelectorAll('.add')
        let inps = document.querySelectorAll('.clearfix input')
        let reduces = document.querySelectorAll('.reduce')
        let prices = document.querySelectorAll('.price')
        let totals = document.querySelectorAll('.total')
        let generalPrice = document.querySelector('.total-price')
        let dels = document.querySelectorAll('.del')
        let totalCount = document.querySelector('#totalCount')

        // 增加数量，计算小计
        for (let i = 0; i < adds.length; i++) {
            totals[i].innerHTML = prices[i].innerHTML
            
            adds[i].addEventListener('click', function () {
                // 点击了谁，就让对应的输入自增
                inps[i].value++
                // 减号要启用
                reduces[i].disabled = false
                // 计算小计模块
                totals[i].innerHTML = parseInt(prices[i].innerHTML) * inps[i].value + '¥'

                getRes()
            })
        }
        // 减少数量，计算小计
        for (let i = 0; i < adds.length; i++) {
            reduces[i].addEventListener('click', function () {
                inps[i].value--
                if (inps[i].value <= 1) {
                    this.disabled = true

                }
                totals[i].innerHTML = parseInt(prices[i].innerHTML) * inps[i].value + '¥'
                getRes()
            })
        }

        for (let i = 0; i < dels.length; i++) {
            dels[i].addEventListener('click', function () {
                this.parentNode.parentNode.remove()
                getRes()
            })

        }
 

        // 计算总价
        function getRes() {
            let inps = document.querySelectorAll('.clearfix input')
            let totals = document.querySelectorAll('.total')
            let sum = 0
            let num = 0
            for (let i = 0; i < totals.length; i++) {
                sum += parseInt(totals[i].innerHTML)
                num += +(inps[i].value)
            }
            generalPrice.innerHTML = sum + '¥'
            // console.log(num);
            totalCount.innerHTML = num
        }
        getRes()

    </script>
</body>

</html>